package me.sdevil507.supports.word;

import me.sdevil507.biz.fruit.po.FruitPO;
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.util.zip.ZipOutputStream;

public class ExportSepcialReport {

    public ExportSepcialReport() {
    }

    public void exportData(String bodyText, ZipOutputStream zipOut) throws IOException {

        StringBuffer sbf = new StringBuffer();
        sbf.append("<html xmlns:v=\"urn:schemas-microsoft-com:vml\" xmlns:o=\"urn:schemas-microsoft-com:office:office\"\n" +
                "xmlns:w=\"urn:schemas-microsoft-com:office:word\" xmlns:m=\"http://schemas.microsoft.com/office/2004/12/omml\"\n" +
                "xmlns=\"http://www.w3.org/TR/REC-html40\"><head>\n" +
                "    <!--[if gte mso 9]><xml><w:WordDocument><w:View>Print</w:View><w:TrackMoves>false</w:TrackMoves><w:TrackFormatting/><w:ValidateAgainstSchemas/><w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid><w:IgnoreMixedContent>false</w:IgnoreMixedContent><w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText><w:DoNotPromoteQF/><w:LidThemeOther>EN-US</w:LidThemeOther><w:LidThemeAsian>ZH-CN</w:LidThemeAsian><w:LidThemeComplexScript>X-NONE</w:LidThemeComplexScript><w:Compatibility><w:BreakWrappedTables/><w:SnapToGridInCell/><w:WrapTextWithPunct/><w:UseAsianBreakRules/><w:DontGrowAutofit/><w:SplitPgBreakAndParaMark/><w:DontVertAlignCellWithSp/><w:DontBreakConstrainedForcedTables/><w:DontVertAlignInTxbx/><w:Word11KerningPairs/><w:CachedColBalance/><w:UseFELayout/></w:Compatibility><w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel><m:mathPr><m:mathFont m:val=\"Cambria Math\"/><m:brkBin m:val=\"before\"/><m:brkBinSub m:val=\"--\"/><m:smallFrac m:val=\"off\"/><m:dispDef/><m:lMargin m:val=\"0\"/> <m:rMargin m:val=\"0\"/><m:defJc m:val=\"centerGroup\"/><m:wrapIndent m:val=\"1440\"/><m:intLim m:val=\"subSup\"/><m:naryLim m:val=\"undOvr\"/></m:mathPr></w:WordDocument></xml><![endif]-->\n" +
                "</head>");
        sbf.append("<body style='line-height: 27pt'>");

        //替换  不然不居中
        bodyText = bodyText.replaceAll("\";text-indent", "\"text-indent");
        bodyText = bodyText.replaceAll("\";text-align", "\"text-align");
        bodyText = bodyText.replaceAll("text-indent:0", "text-indent:0px");
        bodyText = bodyText.replaceAll("text-indent: 0", "text-indent:0px");

        sbf.append(bodyText); //富文本内容
        sbf.append("</body></html>");//缺失的尾标签

        WordUtil.exportWords(sbf.toString(), zipOut);
    }

    /**
     * 换行
     * @param document
     */
    public void addBreak(XWPFDocument document){
        XWPFParagraph xp = document.createParagraph();
        XWPFRun hhr = xp.createRun();
        hhr.setText("\r");
    }

    public String getVal(String val){
        return !StringUtils.isEmpty(val) ? val + "\r" : "";
    }
}